import json

from django.views import View
from .models import Student
from django.http import JsonResponse


# POST /students/ 添加一个学生信息
# GET /students/获取所有学生信息
# GET /students/<pk>/获取一个学生信息
# PUT /students/<pk>/更新一个学生信息
# DElETE /students/<pk>/删除一个学生信息

class StudentView(View):
    """学生视图 """

    def post(self, request):
        """添加一个学生信息"""
        # 接收客户端提交的数据
        name = request.POST.get("name")
        sex = True if request.POST.get("sex") == "1" else False
        age = request.POST.get("age")
        classmate = request.POST.get("classmate")
        description = request.POST.get("description")
        # 验证客户端数据
        # 操作数据库保存数据
        studen = Student.objects.create(name=name, sex=sex, age=age, classmate=classmate, description=description)
        # 返回结果
        return JsonResponse(data={
            "id": studen.pk,
            "name": name,
            "sex": sex,
            "age": age,
            "classmate": classmate,
            "description": description
        }, status=201)

    def get(self, request):
        """获取多个学生信息"""
        # 读取数据库
        students = list(Student.objects.values())
        # 返回数据
        return JsonResponse(data=students, status=200, safe=False)


class StudentInfoView(View):
    def get(self, request, pk):
        """查询单个学生数据"""
        try:
            instance = Student.objects.get(pk=pk)
            return JsonResponse(data={
                "id": instance.pk,
                "name": instance.name,
                "sex": instance.sex,
                "age": instance.age,
                "classmate": instance.classmate,
                "description": instance.description
            }, status=200)
        except Student.DoesNotExist:
            return JsonResponse(data={}, status=404)

    def put(self, request, pk):
        """更新一个学生信息"""
        # 接收客户端提交的数据
        data = json.loads(request.body)
        print(f"data={data}")
        name = data.get("name")
        sex = data.get("sex")
        age = data.get("age")
        classmate = data.get("classmate")
        description = data.get("description")

        # 验证客户端数据
        # 操作数据库保存数据
        try:
            instance = Student.objects.get(pk=pk)
            instance.name = name
            instance.sex = sex
            instance.age = age
            instance.classmate = classmate
            instance.description = description
            instance.save()
        except Exception:
            return JsonResponse(data={}, status=404)
        else:
            # 返回结果
            return JsonResponse(data={
                "id": instance.pk,
                "name": name,
                "sex": sex,
                "age": age,
                "classmate": classmate,
                "description": description
            }, status=201)

    def delete(self, request, pk):
        """删除数据"""
        try:
            Student.objects.filter(pk=pk).delete()
        except:
            pass
        return JsonResponse(data={}, status=204, )
